﻿// Picturez
//
// Copyright © Frank Nagl, 2008-2012
// admin@franknagl.de
// www.franknagl.de
//
namespace Picturez
{
    using System;
    using System.Windows.Forms;
    using SBIP.Forms;

    partial class ImageCutForm
    {
        #region Button click events

        private void ButtonOkClick(object sender, EventArgs e)
        {
            ProcessImage(imageCut.GetCuttedImage());
        }

        private void ButtonRatioClick(object sender, EventArgs e)
        {
            //Trick to get the ratio handling 
            imageCut.MoveSlider(0, Slider.Left);
        }

        #endregion Button click events

        #region ValueChanged events

        private void DownNumericUpDownValueChanged(object sender, EventArgs e)
        {
            Current.Bottom = (int)downNumericUpDown.Value;
            if (imageCut.Focused && !isSettingGuiToCurrentFastcut)
                return;

            if (Current.Bottom + imageCut.TopPosition >
                imageCut.OriginalHeight - imageCut.MinSizeOfImage)
            {
                downNumericUpDown.Value =
                    imageCut.OriginalHeight - imageCut.BottomPosition;
            }
            else
            {
                imageCut.MoveSlider((imageCut.OriginalHeight -
                                    imageCut.BottomPosition) - Current.Bottom,
                                    Slider.Bottom);
            }
        }

        private void LeftNumericUpDownValueChanged(object sender, EventArgs e)
        {
            Current.Left = (int)leftNumericUpDown.Value;
            if (imageCut.Focused && !isSettingGuiToCurrentFastcut) 
                return;

            if (imageCut.RightPosition - Current.Left < imageCut.MinSizeOfImage)
            {
                leftNumericUpDown.Value = imageCut.LeftPosition;
            }
            else
            {
                imageCut.MoveSlider(Current.Left - imageCut.LeftPosition, 
                                    Slider.Left);
            }
        }

        private void RightNumericUpDownValueChanged(object sender, EventArgs e)
        {
            Current.Right = (int)rightNumericUpDown.Value;
            if (imageCut.Focused && !isSettingGuiToCurrentFastcut)
                return;

            if (Current.Right + imageCut.LeftPosition >
                imageCut.OriginalWidth - imageCut.MinSizeOfImage)
            {
                rightNumericUpDown.Value =
                    imageCut.OriginalWidth - imageCut.RightPosition;
            }
            else
            {
                imageCut.MoveSlider((imageCut.OriginalWidth -
                                    imageCut.RightPosition) - Current.Right, 
                                    Slider.Right);
            }
        }

        private void UpNumericUpDownValueChanged(object sender, EventArgs e)
        {
            Current.Top = (int)upNumericUpDown.Value;
            if (imageCut.Focused && !isSettingGuiToCurrentFastcut)
                return;

            if (imageCut.BottomPosition - Current.Top < imageCut.MinSizeOfImage)
            {
                upNumericUpDown.Value = imageCut.TopPosition;
            }
            else
            {
                imageCut.MoveSlider(Current.Top - imageCut.TopPosition, 
                                    Slider.Top);
            }
        }        

        private void RotationNumericUpDownValueChanged(object sender, EventArgs e)
        {
            // return, when value not changed 
            // ..(e.g. by KeyUp-event with Tab or cursor key)
            if (Current.Rotation == RotationNumericUpDown.Value)
                return;

            Current.Rotation = (int)RotationNumericUpDown.Value;
            if (Current.Rotation < 0)
            {
                Current.Rotation += 3600;
            }

            // Original angle
            if (Current.Rotation == 0 || Current.Rotation == 3600)
            {
                ProcessImage(NewBitmapFromPath());
            }
            else
            {
                RotateBilinear filter = new RotateBilinear(
                                    (3600 - Current.Rotation) / 10.0, false);
                ProcessImage(filter.Apply(NewBitmapFromPath()));
            }             
        }
        
        #endregion ValueChanged events

        #region KeyUp events
        
        private void RotationNumericUpDownKeyUp(object sender, KeyEventArgs e)
        {
            RotationNumericUpDownValueChanged(sender, e);

            //Sets the focus to the ImageCut-Control, 
            //..otherwise the key navigation would not work
            if (e.KeyCode == Keys.Return)
                imageCut.Focus();
        }

        private void LeftNumericUpDownKeyUp(object sender, KeyEventArgs e)
        {
            LeftNumericUpDownValueChanged(sender, e);

            //Sets the focus to the ImageCut-Control, 
            //..otherwise the key navigation would not work
            if (e.KeyCode == Keys.Return)
                imageCut.Focus();
        }

        private void RightNumericUpDownKeyUp(object sender, KeyEventArgs e)
        {
            RightNumericUpDownValueChanged(sender, e);

            //Sets the focus to the ImageCut-Control, 
            //..otherwise the key navigation would not work
            if (e.KeyCode == Keys.Return)
                imageCut.Focus();
        }

        private void UpNumericUpDownKeyUp(object sender, KeyEventArgs e)
        {
            UpNumericUpDownValueChanged(sender, e);

            //Sets the focus to the ImageCut-Control, 
            //..otherwise the key navigation would not work
            if (e.KeyCode == Keys.Return)
                imageCut.Focus();
        }

        private void DownNumericUpDownKeyUp(object sender, KeyEventArgs e)
        {
            DownNumericUpDownValueChanged(sender, e);

            //Sets the focus to the ImageCut-Control, 
            //..otherwise the key navigation would not work
            if (e.KeyCode == Keys.Return)
                imageCut.Focus();
        }

        #endregion KeyUp events

        #region Text-, Index-, CheckedChanged events

        private void ComboBoxRatioSelectedIndexChanged(object sender, EventArgs e)
        {
            if (mComboBox_ratio.Text == language.Text[12])
            {
                imageCut.Ratio = 0.0f;
                mRadioButton_landscapeFormat.Enabled = false;
                mRadioButton_panelFormat.Enabled = false;
                mButton_ratio.Enabled = false;
                return;
            }

            mRadioButton_landscapeFormat.Enabled = true;
            mRadioButton_panelFormat.Enabled = true;
            mButton_ratio.Enabled = true;
            string tData = mComboBox_ratio.Text;
            float tNumber1 = float.Parse(tData.Substring(0, tData.IndexOf(' ')));
            float tNumber2 = float.Parse(
                tData.Substring(tData.LastIndexOf(' '),
                                    tData.Length - tData.LastIndexOf(' ')));
            if (isLandScapeFormat)
                imageCut.Ratio = tNumber1 / tNumber2;
            else
                imageCut.Ratio = tNumber2 / tNumber1;

            //Trick to get the ratio handling
            imageCut.MoveSlider(0, Slider.Left);
        }

        private void ComboBoxLineStrengthTextChanged(object sender, EventArgs e)
        {
            byte strength;
            if (!byte.TryParse(ComboBox_LineStrength.Text, out strength) || strength > 7)
                ComboBox_LineStrength.Text = "3";

            if (isComboBoxTextChangedEventSetted)
            {
                string text = "LineStrength;" + ComboBox_LineStrength.Text;
                IOFile.WriteLine(Program.OptionsFile, 4, text, true);

                //Button_OK_Click(null, null);
                imageCut.LineStrength = strength;
                imageCut.Refresh();
            }
        }        

        private void RadioButtonLandscapeFormatCheckedChanged(object sender, EventArgs e)
        {
            isLandScapeFormat = !isLandScapeFormat;
            ComboBoxRatioSelectedIndexChanged(sender, e);
        }
        
        #endregion Text-, Index-, CheckedChanged events
    }
}
